package com.tengju.user.infrastructure.repository.archive;

import com.tengju.user.domain.model.archive.resume.ResumeRepository;
import com.tengju.user.domain.model.archive.resume.Resume;
import com.tengju.user.domain.model.archive.resume.ResumeId;
import com.tengju.user.domain.model.user.UserId;
import com.tengju.user.domain.shared.BizException;
import com.tengju.user.infrastructure.persistence.mybatis.dataobject.ArchiveResumeDO;
import com.tengju.user.infrastructure.persistence.mybatis.mapper.dealer.custom.ArchiveResumeMapper;
import com.tengju.user.infrastructure.repository.convert.ResumeConvert;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

@Repository
public class ResumeRepositoryImpl implements ResumeRepository {

    @Resource
    private ArchiveResumeMapper archiveResumeMapper;

    @Override
    public Resume find(UserId userId) {
        ArchiveResumeDO resumeDO = archiveResumeMapper.queryByUserId(userId.getValue());
        return ResumeConvert.doToModel(resumeDO);
    }

    @Override
    public void store(Resume resume) {
        archiveResumeMapper.insert(ResumeConvert.modelToDo(resume));
    }

    @Override
    public void update(ResumeId resumeId, Resume resume) {
        if (resumeId == null || !resumeId.sameValueAs(resume.getResumeId())) {
            throw new BizException("resumeId不一致");
        }
        archiveResumeMapper.update(ResumeConvert.modelToDo(resume));
    }

}
